package com.mrd.shiptask_shipowner.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;

import com.mrd.shiptask_shipowner.R;
import com.mrd.shiptask_shipowner.activity.ElectronManifestActivity;
import com.mrd.shiptask_shipowner.constants.database.DBUtil;
import com.mrd.shiptask_shipowner.serviecs.initdatadao.InitDatasDaoImpl;
import com.mrd.shiptask_shipowner.unit_tools.DatasTools;
import com.mrd.shiptask_shipowner.unit_tools.FunctionOfActivity;
import com.mrd.shiptask_shipowner.unit_tools.flesh_listview.MyListView;
import com.mrd.shiptask_shipowner.unit_tools.flesh_listview.MyListView.OnRefreshListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ElectroncompletedFragment extends Fragment {
	private List<Map<String, Object>> mData;
	private MyAdapter adapter;
	private MyListView listView2;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(
				R.layout.electron_mainifest_completed_main, container, false);
		setInitView(view);
		setInitDatas(view);
		return view;
	}

	private void setInitView(View view) {
		// TODO Auto-generated method stub

	}

	public void setInitDatas(View view) {
		initListView(view);
	}

	private void initListView(View view) {
		// TODO Auto-generated method stub
		try {
			// true为已经完成的订单，false为未完成的订单
			JSONArray jas = DBUtil.selectElectronTable(getActivity(), true);
			List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < jas.length(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				String getID = DatasTools.getJsonValue(jas.getJSONObject(i),
						"ID");
				String getCODE = DatasTools.getJsonValue(jas.getJSONObject(i),
						"CODE");
				String getSHIPPER = DatasTools.getJsonValue(
						jas.getJSONObject(i), "SHIPPER");
				String getGOODS = DatasTools.getJsonValue(jas.getJSONObject(i),
						"GOODS");
				String getAMOUNT = DatasTools.getJsonValue(
						jas.getJSONObject(i), "AMOUNT");
				String getEND_PORT = DatasTools.getJsonValue(
						jas.getJSONObject(i), "END_PORT");
				String getSTART_PORT = DatasTools.getJsonValue(
						jas.getJSONObject(i), "START_PORT");
				String getCLOSSING_DATE = DatasTools.getJsonValue(
						jas.getJSONObject(i), "CLOSSING_DATE");
				String getSHIPPER_CONFIRM = DatasTools.getJsonValue(
						jas.getJSONObject(i), "SHIPPER_CONFIRM");
				String getOWNER_CONFIRM = DatasTools.getJsonValue(
						jas.getJSONObject(i), "OWNER_CONFIRM");
				map.put("getID", getID);
				map.put("getCODE", getCODE);
				map.put("getSHIPPER", getSHIPPER);
				map.put("getGOODS", getGOODS);
				map.put("getAMOUNT", getAMOUNT);
				map.put("getEND_PORT", getEND_PORT);
				map.put("getSTART_PORT", getSTART_PORT);
				map.put("getCLOSSING_DATE", getCLOSSING_DATE);
				map.put("getSHIPPER_CONFIRM", getSHIPPER_CONFIRM);
				map.put("getOWNER_CONFIRM", getOWNER_CONFIRM);
				mapList.add(map);
			}

			listView2 = (MyListView) view.findViewById(R.id.listView2);
			// 生成动态数组，并且转入数据
			mData = mapList;
			// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
			adapter = new MyAdapter(getActivity());
			listView2.setAdapter(adapter);
			listView2.setonRefreshListener(new OnRefreshListener() {

				@Override
				public void onRefresh() {
					new AsyncTask<Void, Void, Void>() {
						@Override
						protected Void doInBackground(Void... params) {
							try {
								Thread.sleep(2000);
								InitDatasDaoImpl
										.getElectronMainifestDatas(getActivity());
							} catch (Exception e) {
								e.printStackTrace();
							}
							return null;
						}

						@Override
						protected void onPostExecute(Void result) {
							adapter.notifyDataSetChanged();
							listView2.onRefreshComplete();
						}
					}.execute(null, null, null);
				}
			});
		} catch (Exception e) {
		}
	}

	public final class ViewHolder {
		private TextView shipperTv, huowuTv, hangxianTv, codeTv, yunliangTv,
				jiezhiTv, statustap1, statustap2;
		private LinearLayout linear2;
	}

	public class MyAdapter extends BaseAdapter {

		private final LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@SuppressLint({ "NewApi", "ResourceAsColor" })
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			ViewHolder holder = null;

			holder = new ViewHolder();
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.electron_listitem,
						null);
				holder.shipperTv = (TextView) convertView
						.findViewById(R.id.shippertv);
				holder.huowuTv = (TextView) convertView
						.findViewById(R.id.huowutv);
				holder.hangxianTv = (TextView) convertView
						.findViewById(R.id.hangxiantv);
				holder.codeTv = (TextView) convertView
						.findViewById(R.id.codetv);
				holder.yunliangTv = (TextView) convertView
						.findViewById(R.id.yunliangtv);
				holder.jiezhiTv = (TextView) convertView
						.findViewById(R.id.jiezhiriqitv);
				holder.statustap1 = (TextView) convertView
						.findViewById(R.id.statustap1);
				holder.statustap2 = (TextView) convertView
						.findViewById(R.id.statustap2);
				holder.linear2 = (LinearLayout) convertView
						.findViewById(R.id.linear2);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			try {
				// 另item颜色分层
				if (position % 2 == 1) {
					holder.linear2.setBackgroundColor(getResources().getColor(
							R.color.light_gray2));
				} else {
					holder.linear2.setBackgroundColor(getResources().getColor(
							R.color.light_gray));
				}
			} catch (Exception e) {
			}
			holder.shipperTv.setText(mData.get(position).get("getSHIPPER")
					.toString());
			holder.huowuTv.setText(mData.get(position).get("getGOODS")
					.toString());
			holder.hangxianTv.setText(mData.get(position).get("getSTART_PORT")
					.toString()
					+ "-" + mData.get(position).get("getEND_PORT").toString());
			holder.codeTv
					.setText(mData.get(position).get("getCODE").toString());
			holder.yunliangTv.setText(mData.get(position).get("getAMOUNT")
					.toString());
			holder.jiezhiTv.setText(mData.get(position).get("getCLOSSING_DATE")
					.toString());
			if ("1".equals(mData.get(position).get("getOWNER_CONFIRM")
					.toString())) {
				holder.statustap1.setText("托运人已确认");
				holder.statustap1.setTextColor(getResources().getColor(
						R.color.already_con));
				if ("1".equals(mData.get(position).get("getSHIPPER_CONFIRM")
						.toString())) {
					holder.statustap2.setVisibility(View.VISIBLE);
					holder.statustap2.setText("您已确认");
					holder.statustap2.setTextColor(getResources().getColor(
							R.color.already_con));
				}
			} else {
				holder.statustap1.setText("托运人已取消合同");
				holder.statustap1.setTextColor(getResources().getColor(
						R.color.already_reject));
				holder.statustap2.setVisibility(View.GONE);
			}

			holder.linear2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					FunctionOfActivity.progressDialogShow(getActivity());
					// TODO Auto-generated method stub
					Intent intent = new Intent(getActivity(),
							ElectronManifestActivity.class);
					intent.putExtra("eleId", mData.get(position).get("getID")
							.toString());
					intent.putExtra("getSHIPPER_CONFIRM", mData.get(position)
							.get("getSHIPPER_CONFIRM").toString());
					intent.putExtra("getOWNER_CONFIRM", mData.get(position)
							.get("getOWNER_CONFIRM").toString());
					startActivity(intent);
				}
			});
			return convertView;
		}
	}

}
